﻿#region Imports
using System;
using System.IO;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
#endregion

namespace DirectoryAnalyzer.Models
{
    public class FInfo
    {
        #region Declarations
        internal const string sTHUMBS_FILE = "Thumbs.db";
        internal const string sTHUMBS_EXT = "db";
        #endregion

        #region Constructors
        protected FInfo()
        {

        }

        public FInfo(string filePath, bool skipHash)
        {
            SkipHash = skipHash;
            PopulateProperties(new FileInfo(filePath));
        }

        public FInfo(FileInfo fileInfo, bool skipHash)
        {
            SkipHash = skipHash;
            PopulateProperties(fileInfo);
        }

        protected FInfo(FDupInfo dupInfo, bool skipHash)
        {
            SkipHash = skipHash;
            PopulateProperties(dupInfo);
        }
        #endregion

        #region Properties
        public bool SkipHash { private get; set; }
        public bool IsFileEmpty { get; private set; }
        public bool IsThumbnail { get; private set; }

        public string FileName { get; private set; }
        public string FilePath { get; private set; }
        public string RelPath { get; private set; }
        public string FileExtn { get; private set; }
        public string FileSize { get { return ByteSize.ToPracticalSize(); } }
        public long ByteSize { get; private set; }
        public string HashCode { get { return HashUtil.GetHashCode(FilePath, SkipHash); } }
        public DateTime CreatedDate { get; set; }
        public DateTime LastModified { get; set; }
        public DateTime LastAccessed { get; set; }
        public string CreatedDateText { get { return CreatedDate.ToString("yyyy-MMM-dd HH:mm:ss"); } }
        public string LastModifiedText { get { return LastModified.ToString("yyyy-MMM-dd HH:mm:ss"); } }
        public string LastAccessedText { get { return LastAccessed.ToString("yyyy-MMM-dd HH:mm:ss"); } }
        #endregion

        #region Methods
        private void PopulateProperties(FileInfo fileInfo)
        {
            FileName = fileInfo.Name;
            FilePath = fileInfo.FullName;
            RelPath = ".." + Path.DirectorySeparatorChar
                + fileInfo.FullName.Replace(fileInfo.DirectoryName, "").Trim(Path.DirectorySeparatorChar);
            ByteSize = fileInfo.GetLength();
            FileExtn = fileInfo.Extension.Trim('.');
            CreatedDate = fileInfo.CreationTime;
            LastModified = fileInfo.LastWriteTime;
            LastAccessed = fileInfo.LastAccessTime;

            IsFileEmpty = (ByteSize == 0);
            IsThumbnail = IsThumbnailFile(fileInfo);
        }

        private void PopulateProperties(FDupInfo dupInfo)
        {
            FileName = dupInfo.FileName;
            FilePath = dupInfo.FilePath;
            RelPath = dupInfo.RelPath;
            FileExtn = dupInfo.FileExtn;
            ByteSize = dupInfo.ByteSize;
            CreatedDate = dupInfo.CreatedDate;
            LastModified = dupInfo.LastModified;
            LastAccessed = dupInfo.LastAccessed;

            IsFileEmpty = (ByteSize == 0);
            IsThumbnail = dupInfo.IsThumbnail;
        }

        private bool IsThumbnailFile(FInfo file)
        {
            return file.FileExtn == sTHUMBS_EXT || file.FileName == sTHUMBS_FILE;
        }

        private bool IsThumbnailFile(FileInfo file)
        {
            return file.Extension.Trim('.') == sTHUMBS_EXT || file.Name == sTHUMBS_FILE;
        }
        #endregion
    }
}
